/*
 * Created on 2005-7-6
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sohu.sip.auth.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sohu.peak.util.Logger;
import com.sohu.peak.util.Pagination;
import com.sohu.sip.auth.business.bean.AuthUserBean;
import com.sohu.sip.auth.business.event.UserAddEvent;
import com.sohu.sip.auth.business.event.UserListEvent;
import com.sohu.sip.auth.business.event.UserUpdateEvent;
import com.sohu.sip.auth.business.result.UserAddResult;
import com.sohu.sip.auth.business.result.UserListResult;
import com.sohu.sip.auth.business.result.UserUpdateResult;
import com.sohu.sip.auth.constant.AuthConstant;
import com.sohu.sip.auth.po.AdminUser;
import com.sohu.sip.auth.util.ParamUtil;

/**
 * @author luciali
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class AuthUserAction extends BaseAction {
    private static Logger log = new Logger();
    
    private Long id;
	private AdminUser adminUser;
	
	private String username;

	private String cnName;

	private String tel;

	private String mark;

	private Integer authStatus;

	private Date lastTime;

	private Date createTime;

//	private Integer status;

    

    public AdminUser getAdminUser() {
		return adminUser;
	}

	public void setAdminUser(AdminUser adminUser) {
		this.adminUser = adminUser;
	}

	public String addUser() {
        return "addUser";
    }

    public String listUser() {
        try {
        	
    		if (getPagination() == null) {
			Pagination pagination = new Pagination(1,20);
			this.setPagination(pagination);
		}

        this.getPagination().setPageCount(20);

    		log.debug("list_Pagination PageNO=%s "+ getPagination().getPageNO());
    		log.debug("list_status = %s"+getStatus());
        	
        	HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
            UserListEvent event = new UserListEvent(ParamUtil.getLowerCase(
                    request, "username"), 1, ParamUtil
                    .getInt(request, "status").intValue(), 
                    this.getPagination().getPageNO(), 
                    this.getPagination().getPageCount());
            UserListResult result = AuthUserBean.getInstance().list(event);
            request.setAttribute("result", result);
            request.setAttribute("pagination", result.getPagination());
            if (result.getStatus() == 0)
                return "listUser";
            else
                return "failure";
        } catch (Exception e) {
            log.error(e);
            return "failure";
        }
    }

    public String listAppUser() {
		try {
			HttpServletRequest request = org.apache.struts2.ServletActionContext
					.getRequest();
			UserListEvent event = new UserListEvent("", ParamUtil.getInt(
					request, "authStatus", 0).intValue(), -1, ParamUtil.getInt(
					request, "pagination.pageNO", 1).intValue(), ParamUtil
					.getInt(request, "pageCount", AuthConstant.COUNT_PERPAGE)
					.intValue());
			UserListResult result = AuthUserBean.getInstance().list(event);
			request.setAttribute("result", result);
			request.setAttribute("pagination", result.getPagination());
			if (result.getStatus() == 0)
				return "listAppUser";
			else
				return "failure";
		} catch (Exception e) {
			log.error(e);
			return "failure";
		}
	}

    public String insertUser() {
        try {
        	HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
        	HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
            UserAddEvent event = new UserAddEvent(ParamUtil.getLowerCase(
                    request, "username"), ParamUtil.getString(request,
                    "password"), ParamUtil.getChineseParam(request, "cnName"),
                    ParamUtil.getString(request, "tel"), ParamUtil.getString(
                            request, "mark"), 1, ParamUtil.getInt(request,
                            "status").intValue());
            UserAddResult result = new UserAddResult();
//            HttpSession session = request.getSession();
//            if (session.getAttribute("noLDAPVerify") == null
//                    && !LDAPVerify.verfifyUser(ParamUtil.getLowerCase(request,
//                            "username"), ParamUtil.getString(request,
//                            "password"))) {
//                result.setStatus(AuthConstant.USER_ILLEGAL);
//                return "failure";
//            }

            result = AuthUserBean.getInstance().add(event);
            request.setAttribute("result", result);

            if (result.getStatus() == 0) {
                response.sendRedirect("authuser_success.sip");
                return null;
            } else {
                return "failure";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "failure";
        }
    }
    
    public String insertPardUser() {
    	try {
    		HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
    		HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();    		 		
    		UserAddResult result = AuthUserBean.getInstance().addPard(ParamUtil.getLowerCase(
    				request, "username"), ParamUtil.getString(request,
    				"password"), ParamUtil.getChineseParam(request, "cnName"), ParamUtil.getString(request, "tel"), ParamUtil.getString(
    						request, "mark"), ParamUtil.getChineseParam(request, "companyname"), ParamUtil.getChineseParam(request, "email"));
    		request.setAttribute("result", result);
    		
    		if (result.getStatus() == 0) {
    			response.sendRedirect("authuser_success.sip");
    			return null;
    		} else {
    			return "failure";
    		}
    	} catch (Exception e) {
    		e.printStackTrace();
    		return "failure";
    	}
    }

    public String modifyUser() {
        try {
        	HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
            UserUpdateEvent event = new UserUpdateEvent(ParamUtil.getLong(
                    request, "id"));
            UserUpdateResult result = AuthUserBean.getInstance().getDetail(
                    event);
            request.setAttribute("result", result);

            if (result.getStatus() == 0)
                return "modifyUser";
            else
                return "failure";
        } catch (Exception e) {
            log.error(e.getMessage());
            return "failure";
        }
    }

    public String modifyAppUser() {
        try {
        	HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
            UserUpdateEvent event = new UserUpdateEvent(ParamUtil.getLong(
                    request, "id"));
            UserUpdateResult result = AuthUserBean.getInstance().getDetail(
                    event);
            request.setAttribute("result", result);

            if (result.getStatus() == 0)
                return "modifyAppUser";
            else
                return "failure";
        } catch (Exception e) {
            log.error(e.getMessage());
            return "failure";
        }
    }

    public String updateUser() {
    	HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
        UserUpdateEvent event = new UserUpdateEvent(ParamUtil.getLong(request,
                "id"), ParamUtil.getChineseParam(request, "cnName"), ParamUtil
                .getString(request, "tel"), ParamUtil
                .getString(request, "mark"), ParamUtil
                .getInt(request, "status").intValue());
        UserUpdateResult result = AuthUserBean.getInstance().update(event,
                ParamUtil.getChineseParam(request, "companyname"),
                ParamUtil.getString(request, "email"));
        request.setAttribute("result", result);
        if (result.getStatus() == 0)
            return "success";
        else
            return "failure";
    }

    public String updateAppUser() {
    	HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
        UserUpdateEvent event = new UserUpdateEvent(ParamUtil.getLong(request,
                "id"), ParamUtil.getChineseParam(request, "cnName"), ParamUtil
                .getString(request, "tel"), ParamUtil
                .getString(request, "mark"), ParamUtil.getInt(request,
                "authStatus").intValue(), ParamUtil
                .getInt(request, "status", 0).intValue(), (String[]) request
                .getParameterValues("actors"));
        UserUpdateResult result = AuthUserBean.getInstance().updateAppPard(
                event, ParamUtil.getChineseParam(request, "companyname"),
                ParamUtil.getString(request, "email"));

            request.setAttribute("result", result);
        if (result.getStatus() == 0)
            return "success";
        else
            return "failure";
    }

    public String modifySelf() {
        try {
        	 HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
            HttpSession session = request.getSession();
            AdminUser adminUser = (AdminUser) session.getAttribute("adminUser");
            UserUpdateEvent event = new UserUpdateEvent(adminUser.getId());
            UserUpdateResult result = AuthUserBean.getInstance().getDetail(
                    event);
            
           
            request.setAttribute("result", result);

            if (result.getStatus() == 0)
                return "modifySelf";
            else
                return "failure";
        } catch (Exception e) {
            log.error(e.getMessage());
            return "failure";
        }
    }

    public String updateSelf() {
    	HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        AdminUser adminUser = (AdminUser) session.getAttribute("adminUser");
        UserUpdateEvent event = new UserUpdateEvent(adminUser.getId(),
                ParamUtil.getChineseParam(request, "cnName"), ParamUtil.getString(
                        request, "tel"), ParamUtil.getString(request, "mark"),
                ParamUtil.getInt(request, "status").intValue());
        UserUpdateResult result = AuthUserBean.getInstance().update(event,
                ParamUtil.getChineseParam(request, "companyname"),
                ParamUtil.getString(request, "email"));
        request.setAttribute("result", result);
        if (result.getStatus() == 0)
            return "success";
        else
            return "failure";
    }

    public String modifyPassword() {
    	HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
        Long id = ParamUtil.getLong(request, "id");
        String oldPassword = ParamUtil.getString(request, "old_password");
        String password = ParamUtil.getString(request, "password");

        if (AuthUserBean.getInstance()
                .modifyPassword(id, oldPassword, password)) {
            return "success";
        } else {
            return "failure";
        }

    }

    public String success() {
        return "success";
    }

    public String failure() {
        return "failure";
    }

	public Integer getAuthStatus() {
		return authStatus;
	}

	public void setAuthStatus(Integer authStatus) {
		this.authStatus = authStatus;
	}

	public String getCnName() {
		return cnName;
	}

	public void setCnName(String cnName) {
		this.cnName = cnName;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getLastTime() {
		return lastTime;
	}

	public void setLastTime(Date lastTime) {
		this.lastTime = lastTime;
	}

	public String getMark() {
		return mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

//	public Integer getStatus() {
//		return status;
//	}
//
//	public void setStatus(Integer status) {
//		this.status = status;
//	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}
